<?php
namespace app\home\controller;

use think\Db;
use think\Request;
use think\Controller;
use think\helper\Str;
use dingtalk\CallBack;
use dingtalk\Auth;
use dingtalk\util\crypto\Prpcrypt;
use dingtalk\util\crypto\DingtalkCrypt;

class AjaxController extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function listInfo(){
        $data = $this->request->param();
        //$list = Db::table($data['table'])->where($data['map'])->select();
        $list = Db::table($data['table'])->where(['user_id'=>$data['map']])->select();
        return $list;
    }
    /**
     * @return mixed
     */
    public function addData(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $res = Db::table($data['table'])->strict(true)->insert($data);
            $this->success('添加成功！');
        }
    }
    public function editData(){
        if($this->request->isAjax()){
            $data = $this->request->param();
            $res = Db::table($data['table'])->strict(true)->update($data);
            $this->success('修改成功！');
        }
    }

    public function index(){
        //return $this->fetch();exit;11
        $auth = new Auth();
        $this->accessToken = $auth::getAccessToken();
        //查询回调变化
        //$res = CallBack::getCallBack($this->getAccessToken());
        //dump($res);exit;

        //认证url参数
        $key = 'abcaaaaabcaaaaabcaaaaabcaaaaabcaaaaabcaaaas';//返回的信息的密钥加密后
        $call = ["call_back_tag"=> ["user_add_org","user_modify_org","user_leave_org","org_admin_add","org_admin_remove","org_dept_create","org_dept_modify","org_dept_remove","org_remove","org_change","label_user_change","label_conf_add","label_conf_modify","label_conf_del",],
            "token"=> "888999",
            "aes_key"=> $key,
            "url"=>"http://dingtalk.180vip.cn/home/ajax/b.html"
        ];
        //加密success
        //$Prpcrypt = new Prpcrypt($key);
        //$success = $Prpcrypt->encrypt('success', config('dingtalk.CORPID'));//加密success
        //dump($success);exit;
        //Stc1d/xe11wVB2bHGP0hblhSnKEo0ITZ4/2NeXdbThUZtvpRs74h8XojzUysGFhhUE7xPs+43unT8XYWx3WY7g==

        //认证url
        $a = CallBack::registerCallBack($this->accessToken,$call);
        exit;

        //解密回调直
        $res = ['signature' => '64c03f8b7f56528c8719f0f1d748182a54c2a37b',
            'timestamp' => '1510901073857',
            'nonce' => 'svTDUitG',
            'encrypt' => 'aDNcad5NHdPwNuugEslgFZD4hQatI09tgwm19d3Ihelh45LEjqD7WLs+hG2k5wSkWMoFdVwsTW1AAxD+sg6KBQ==',];
        //dump(json_encode($res));exit;
        $Prpcrypt = new Prpcrypt($key);
        $encrypt = $Prpcrypt->decrypt('oAwVHqzNi2ubhovyxhCeavjm63uzjMjMaZ+/WdaX4LaHa6BTbKKWoDRIDmFg7Zid', config('dingtalk.CORPID'));
        $encrypt = json_decode($encrypt[1]);
        dump($encrypt);exit;
        return $this->fetch();
    }

    public function b(){
        $data = $this->request->param();
        trace($data,'log');
        trace('888999','log');
        if($this->request->has('signature')){
            $data = $this->request->param();
            cache('aaa',$data);
            trace($data,'log');
        }
        $data = cache('aaa');
        $key = 'abcaaaaabcaaaaabcaaaaabcaaaaabcaaaaabcaaaas';
        $DingtalkCrypt = new DingtalkCrypt('888999',$key,config('dingtalk.CORPID'));
        $encryptMsg = '';
        $encrypt = $DingtalkCrypt->EncryptMsg('success', $data['timestamp'], $data['nonce'], $encryptMsg);
        return $encryptMsg;
        //$encryptMsg = json_encode($encryptMsg);
        //dump($encryptMsg);

        /*
        $key = Base64_Decode('abcaaaaabcaaaaabcaaaaabcaaaaabcaaaaabcaaaas');
        $success = Str::random(16).pack("N", strlen('success')).'success'.config('dingtalk.CORPID');
        $success = openssl_encrypt($success, 'des-ede3', $key, OPENSSL_RAW_DATA);
        $success = base64_encode($success);
        cache('d',$success);
        $a['msg_signature'] = $data['signature'];
        $a['timeStamp'] = $data['timestamp'];
        $a['nonce'] = $data['nonce'];
        $a['encrypt'] = $success;
        return json_encode($a);
        */
    }
    public function c(){
        dump(cache('a'));
    }
}
